/*
 * Copyright (C) 2010 Zaizi Limited.
 *
 * Alfresco FISE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco FISE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco FISE. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Document info component.
 * 
 * @namespace Alfresco
 * @class Alfresco.DocumentSemantic
 */
(function() {
	/**
	 * YUI Library aliases
	 */
	var Dom = YAHOO.util.Dom;

	/**
	 * Alfresco Slingshot aliases
	 */
	var $html = Alfresco.util.encodeHTML;

	/**
	 * DocumentSemantic constructor.
	 * 
	 * @param {String}
	 *            htmlId The HTML id of the parent element
	 * @return {Alfresco.DocumentSemantic} The new DocumentSemantic instance
	 * @constructor
	 */
	Alfresco.DocumentSemantic = function(htmlId) {
		Alfresco.DocumentSemantic.superclass.constructor.call(this,
				"Alfresco.DocumentSemantic", htmlId);

		return this;
	};

	YAHOO.extend(Alfresco.DocumentSemantic, Alfresco.component.Base, {
		// Ontologies
	    TYPE_PLACE : "http://dbpedia.org/ontology/Place",
	    TYPE_ORGANISATION : "http://dbpedia.org/ontology/Organisation",
	    TYPE_PERSON : "http://dbpedia.org/ontology/Person",
	    
		/**
		 * Fired by YUI when parent element is available for scripting.
		 * Component initialisation, including instantiation of YUI widgets and
		 * event listener binding.
		 * 
		 * @method onReady
		 */
		onReady : function EDS_onReady() {
			// Request semantic information
			Alfresco.util.Ajax.request({
				url : Alfresco.constants.PROXY_URI +
						"zaizi/fiseiks/semantic?nodeRef=" + this.options.nodeRef,
				successCallback : {
					fn : this.onSemanticLoaded,
					scope : this
				},
				failureMessage : {
					fn : this.onSemanticFailure,
					scope : this
				}
			});
		},

		/**
		 * Success semantic data loaded
		 * 
		 * @param res {object} Response
		 * @method onSemanticLoaded
		 */
		onSemanticLoaded : function DocumentSemantic_onSemanticLoaded(res) {
			var content = YAHOO.lang.JSON.parse(res.serverResponse.responseText);

			var entities = content.entities || {}
			
			var people = entities[this.TYPE_PERSON] || [];
			this._renderSection('people', people);
			
			var organizations = entities[this.TYPE_ORGANISATION] || [];
			this._renderSection('organizations', organizations);
			
			var places = entities[this.TYPE_PLACE] || [];
			this._renderSection('places', places);
		},

		/**
		 * Failure in semantic data loading
		 * 
		 * @param res {object} Response
		 * @method onSemanticFailure
		 */
		onSemanticFailure : function DocumentSemantic_onSemanticFailure(res) {
			Dom.setStyle(this.id + "-body", 'visibility', 'hidden');
		},
		
		/**
		 * Create a list entry for one entity
		 * 
		 * @param entity {object} Entity information
		 * @method _createEntity
		 */
		_createEntity: function DocumentSemantic__createItem(entity) {
			return '<li class="meta-value">' +
				'<a href="documentlibrary#filter=entity|' + entity.label + '&page=1">' + entity.label + '</a>' +
				'</li>';
		},
		
		/**
		 * Render a semantic section
		 * 
		 * @param sec {string} Section
		 * @param entities {array} List of entities
		 * @method _renderSection
		 */
		_renderSection: function DocumentSemantic__renderSection(sec, entities) {
			var secid = this.id + '-' + sec;
			
			if(entities.length != 0) {
				var o = '<ul>';
				for(var i=0; i<entities.length; i++) {
					o += this._createEntity(entities[i]);
				}
				o += '</ul>';
				
				Dom.get(secid).innerHTML = o;
			}
		}
	});
})();